<?php 
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Member\Controller;
use \Member\Controller\InitController;
class ScoreController extends InitController {
	
	public function _initialize() {
		parent::_initialize();
		$this->bean_db = D('Score');
	}
	
	
	/*账户明细页面*/
	public function account(){
		$form =  new \Common\Library\form();
		$info=I('param.');
		$type = I('type', 'u_coin');
		$start=strtotime($info['start_addtime']);
		$end=strtotime($info['end_addtime']);
		$pagecurr = max(1,I('page',0,'intval'));
		$pagesize = 10;
		$sqlmap=array();
		$sqlmap['userid']=$this->userid;
		if ($type == 'u_coin'){
		$sqlmap['u_type']=array('NEQ','u_bean');
		}
		if ($type == 'u_bean'){
		$sqlmap['u_type']=$type;
		}
		if ($start&&$end){
			 $end += 86400;
		$sqlmap['posttime']=array(array('EGT',$start),array('lt',$end),'and');
		}elseif($start){
		$sqlmap['posttime']=array(array('EGT',$start));
		}elseif ($end){
			 $end += 86400;
		$sqlmap['posttime']=array(array('lt',$end));
			
		}
		$count = $this->bean_db->where($sqlmap)->count();
        $account=$this->bean_db->where($sqlmap)->page($pagecurr,$pagesize)->order("id DESC")->select();
		$pages = page($count,$pagesize);
		$SEO=seo(0,"我的账户明细信息");
		$beginThismonth=date("Y-m-d",mktime(0,0,0,date('m'),1,date('Y'))); 
		$endThismonth=date("Y-m-d",mktime(23,59,59,date('m'),date('t'),date('Y')));
		include template('account');
	}
	
	/*u豆明细*/
	public function u_bean(){
		$info = I('get.');
		$form =  new \Common\Library\form();
		$start=strtotime($info['start_addtime']);
		$end=strtotime($info['end_addtime']);
		$pagecurr = max(1,I('page',0,'intval'));
		$pagesize = 10;
		$sqlmap=array();
		$sqlmap['userid']=$this->userid;
		$sqlmap['u_type']="u_bean";
		if ($start&&$end){
			 $end += 86400;
		$sqlmap['posttime']=array(array('EGT',$start),array('lt',$end),'and');
		}elseif($start){
		$sqlmap['posttime']=array(array('EGT',$start));
		}elseif ($end){
			 $end += 86400;
		$sqlmap['posttime']=array(array('lt',$end));
			
		}
		$count=$this->bean_db->where($sqlmap)->count();
		$bean=$this->bean_db->where($sqlmap)->page($pagecurr,$pagesize)->select();
		$beginThismonth = date("Y-m-d",mktime(0,0,0,date('m'),1,date('Y'))); 
		$endThismonth = date("Y-m-d",mktime(23,59,59,date('m'),date('t'),date('Y')));
		//echo $this->bean_db->getLastSql();
		$SEO=seo(0,"U豆明细信息");
		$pages = page($count,$pagesize);
		include template('u_bean');
	}
	

	/*u币兑换u豆页面*/
	public function exc_bean(){
		$SEO=seo(0,"U币兑换U豆信息");
		$info=$this->db->where(array('userid'=>$this->userid))->find();
		include template('exc_bean');
	}
	
	/*u币兑换*/
	public function change_bean(){
		$info=I('post.');
		$bean=$info['v']*10000;
		$pwd=htmlspecialchars($info['p']);
		$config = getcache('setting', 'member');
		$exc_bean=$config['exc_bean'];
		$user=$this->db->where(array('userid'=>$this->userid))->find();
		if ($user['u_coin']<$bean){
				$data['flag']=0;
				$data['msg']="你当前的U币不足以兑换！";
				echo json_encode($data);
		
		}
		elseif ($user['password']!=md5(md5($pwd.$user['encrypt']))){
				$data['flag']=0;
				$data['msg']="密码输入有误";
				echo json_encode($data);
		}else{
			$u_bean=$user['u_bean']+$bean*$exc_bean;
			$coin=$user['u_coin']-$bean;
			$result=$this->db->where(array('userid'=>$this->userid))->save(array('u_coin'=>$coin,'u_bean'=>$u_bean));
			$bean_info=score_change($this->userid,'u币兑换u豆',$bean*$exc_bean,'u_bean',1);
			$u_coin=score_change($this->userid,'u币兑换u豆',$bean,'u_coin',0);
			if ($result && $bean_info){
				$users=$this->db->where(array('userid'=>$this->userid))->find();
				$data['flag']=1;
				$data['gold']=$users['u_coin'];
				$data['dou']=$users['u_bean'];
				$data['getDou']=$bean*$exc_bean;
				$data['msg']="兑换成功！";
				echo json_encode($data);
			}
		}
		
	}
	
	
}